package com.ruoyi.system.service.project.impl;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.project.paper.PaperInfo;
import com.ruoyi.system.domain.project.paper.PaperInfoTeacher;
import com.ruoyi.system.mapper.project.PaperInfoMapper;
import com.ruoyi.system.mapper.project.PaperInfoTeacherMapper;
import com.ruoyi.system.service.project.IPaperInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 论文信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-27
 */
@Service
public class PaperInfoServiceImpl implements IPaperInfoService
{
    @Autowired
    private PaperInfoMapper paperInfoMapper;
    @Autowired
    private PaperInfoTeacherMapper paperInfoTeacherService;
    /**
     * 查询论文信息
     * 
     * @param id 论文信息主键
     * @return 论文信息
     */
    @Override
    public PaperInfo selectPaperInfoById(Long id)
    {
        PaperInfo paperInfo = paperInfoMapper.selectPaperInfoById(id);
        PaperInfoTeacher ra = new PaperInfoTeacher();
        ra.setPaperId(id);
        List<PaperInfoTeacher> teachers = paperInfoTeacherService.selectPaperInfoTeacherList(ra);
        paperInfo.setTeachers(teachers);
        return paperInfo;
    }

    /**
     * 查询论文信息列表
     * 
     * @param paperInfo 论文信息
     * @return 论文信息
     */
    @Override
    public List<PaperInfo> selectPaperInfoList(PaperInfo paperInfo)
    {
        //如果是老师，则只显示自己上传的
        if(paperInfo.getCurrentRole()!=null&&paperInfo.getCurrentRole().equals("teacher")){
            paperInfo.setAuthorId( getUserId() );
        }
        //如果是部门，则显示老师是系里的所有
        if(paperInfo.getCurrentRole()!=null&&paperInfo.getCurrentRole().equals("department")){
            paperInfo.setDeptId(SecurityUtils.getDeptId());
        }
        List<PaperInfo> paperInfos = paperInfoMapper.selectPaperInfoList(paperInfo);
        //获取作者信息
        for(PaperInfo ra : paperInfos){
            PaperInfoTeacher teacher = new PaperInfoTeacher();
            teacher.setPaperId(ra.getId());
            List<PaperInfoTeacher> teachers = paperInfoTeacherService.selectPaperInfoTeacherList(teacher);
            ra.setTeachers(teachers);
        }
        return paperInfos;
    }

    /**
     * 新增论文信息
     * 
     * @param paperInfo 论文信息
     * @return 结果
     */
    @Override
    public int insertPaperInfo(PaperInfo paperInfo)
    {
        if(paperInfo.getId()!=null){
            PaperInfoTeacher teacher = new PaperInfoTeacher();
            teacher.setPaperId(paperInfo.getId());
            teacher.setRank((long) paperInfo.getRank());
            teacher.setUserId(String.valueOf(paperInfo.getAuthorId()));
            teacher.setUserType(String.valueOf(paperInfo.getAuthorType()));
            teacher.setUserName(paperInfo.getAuthorName());
            paperInfoTeacherService.insertPaperInfoTeacher( teacher);
        }else {
            int result = paperInfoMapper.insertPaperInfo(paperInfo);
            if(result>0){
                PaperInfoTeacher teacher = new PaperInfoTeacher();
                teacher.setPaperId(paperInfo.getId());
                teacher.setRank((long) paperInfo.getRank());
                teacher.setUserId(String.valueOf(paperInfo.getAuthorId()));
                teacher.setUserType(String.valueOf(paperInfo.getAuthorType()));
                teacher.setUserName(paperInfo.getAuthorName());
                paperInfoTeacherService.insertPaperInfoTeacher( teacher);
            }
        }
        return 1;
    }

    /**
     * 修改论文信息
     * 
     * @param paperInfo 论文信息
     * @return 结果
     */
    @Override
    public int updatePaperInfo(PaperInfo paperInfo)
    {
        return paperInfoMapper.updatePaperInfo(paperInfo);
    }

    /**
     * 批量删除论文信息
     * 
     * @param ids 需要删除的论文信息主键
     * @return 结果
     */
    @Override
    public int deletePaperInfoByIds(Long[] ids)
    {
        for(Long id : ids){
            PaperInfoTeacher teacher = new PaperInfoTeacher();
            teacher.setPaperId(id);
            List<PaperInfoTeacher> teachers = paperInfoTeacherService.selectPaperInfoTeacherList(teacher);
            if(!teachers.isEmpty()){
                for (PaperInfoTeacher paperInfoTeacher : teachers) {
                    paperInfoTeacherService.deletePaperInfoTeacherById(paperInfoTeacher.getId());
                }
            }
        }
        return paperInfoMapper.deletePaperInfoByIds(ids);
    }

    /**
     * 删除论文信息信息
     * 
     * @param id 论文信息主键
     * @return 结果
     */
    @Override
    public int deletePaperInfoById(Long id)
    {
        return paperInfoMapper.deletePaperInfoById(id);
    }
    /**
     * 根据标题查询论文信息
     * @param data
     * @return
     */
    @Override
    public PaperInfo getPaperInfoByTitle(String data) {
        return paperInfoMapper.getPaperInfoByTitle( data);
    }
}
